package com.atguigu.gmall.file;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 异步 编排
 */
public class CompletableFutureDemo3 {

    public static ThreadPoolExecutor threadPoolExecutor =
            new ThreadPoolExecutor(4,
                    5,
                    10,
                    TimeUnit.SECONDS,
                    new ArrayBlockingQueue(100)

            );
    //创建一个多线程
    public static void main(String[] args) throws Exception {

        //1:创建线程对象  新线程
        CompletableFuture result = CompletableFuture.supplyAsync(() -> {//1
            System.out.println(Thread.currentThread().getName() + ":1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //int i = 1/0;
            return "one";
        }, threadPoolExecutor);
        //2:并行了
        result.thenApplyAsync((t) -> {//2
            System.out.println(Thread.currentThread().getName()+ ":2");
            System.out.println("thenApplyAsync:t:" + t);
            return "two";
        },threadPoolExecutor);
        //3:并行了
        result.thenAcceptAsync((t) -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+ ":3");
            System.out.println("thenAcceptAsync:t:" + t);
        },threadPoolExecutor);
    }
}
